<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="danger" size='small' @click="clearData" class="clearData">{{$t('清空')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="dnatList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="policyname" :label="$t('策略名称')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.policyname" :content="scope.row.policyname" class="wid190" :refName="scope.row.policyname">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="iptype" :label="$t('IP类型')" align='center' width="100">
        <template slot-scope="scope">
          <tooltip :html="scope.row.iptype" :content="scope.row.iptype" class="wid190" :refName="scope.row.iptype">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="addrpool" :label="$t('地址池')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.addrpool" :content="scope.row.addrpool" class="wid190" :refName="scope.row.addrpool">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="port" :label="$t('端口')" align='center'></el-table-column>
      <el-table-column prop="sip" :label="$t('源IP')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.sip" :content="scope.row.sip" class="wid190" :refName="scope.row.sip">
          </tooltip>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="proto" :label="$t('协议类型')" align='center' width="100">
        <template slot-scope="scope">
          {{protoList[scope.row.proto]}}
        </template>
      </el-table-column>
      <el-table-column prop="sport" :label="$t('源端口号')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.sport" :content="scope.row.sport" class="wid190" :refName="scope.row.sport">
          </tooltip>
        </template>
      </el-table-column> -->
      <el-table-column prop="service" :label="$t('服务名称')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.service" :content="scope.row.service" class="wid190" :refName="scope.row.service">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="dip" :label="$t('目的IP')" align='center'>
        <template slot-scope="scope">
          <tooltip :html="scope.row.dip" :content="scope.row.dip" class="wid190" :refName="scope.row.dip">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="PolicyEnableOrNot" :label="$t('策略使能')" align='center' width="120">
        <template slot-scope="scope">
          {{PolicyEnableOrNotList[scope.row.PolicyEnableOrNot]}}
        </template>
      </el-table-column>
      <el-table-column prop="LogOpenOrNot" :label="$t('日志使能')" align='center' width="100">
        <template slot-scope="scope">
          {{LogOpenOrNotList[scope.row.LogOpenOrNot]}}
        </template>
      </el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="100">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <dnat-dialog v-if="dnatDialogVisible" ref="refDnatDialogVisible"></dnat-dialog>
  </div>
</template>
<script>
import { getDnatArray, delDnat, clearDnat } from '@/api/network/address/dnat'
import DnatDialog from './components/DnatDialog'
// import 'view-design/dist/styles/iview.css'
export default {
  name: 'Dnat',
  components: {
    DnatDialog
  },
  data () {
    return {
      dnatDialogVisible: false,
      dnatList: [],
      record: {
        page: 1,
        limit: 20
      },
      total: 0,
      nameStr: '',
      LogOpenOrNotList: {
        'on': this.$t('开'),
        'off': this.$t('关')
      },
      PolicyEnableOrNotList: {
        'yes': this.$t('开'),
        'no': this.$t('关')
      },
      protoList: {
        'nothing': this.$t('未配置'),
        'tcp': 'tcp',
        'udp': 'udp',
        'dccp': 'dccp',
        'sctp': 'sctp'
      }
    }
  },
  created () {
    this.listData()
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.record.limit = val
      this.record.page = 1
      this.listData()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.record.page = val
      this.listData()
    },
    // 列表
    listData () {
      getDnatArray(this.record).then(res => {
        if (res.status === 0) {
          this.total = res.count
          this.dnatList = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.dnatDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refDnatDialogVisible.init(data)
      })
    },
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.policyname
        } else {
          nameStr = nameStr + '@#@' + item.policyname
        }
      }
      this.nameStr = nameStr
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delDnat({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    clearData () {
      this.$confirm(this.$t('您是否要清空所有数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        clearDnat().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('清空成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    }
  }
}
</script>
